// learn1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>

//struct Arr
//{
//	int * pBase;
//	int cnt;//已有
//	int len;
//};
//void init_arr(Arr * p,int len,int cnt)//初始化
//{
//	p->pBase= (int *)malloc(sizeof(int)*len);
//	p->len = len;
//	p->cnt = cnt;
//	for (int i = 0; i < cnt; i++)
//	{
//		*(p->pBase + i) = 1;
//	}
//	return;
//}
//int get(Arr * p, int pos){//获取元素
//	return *(p->pBase + pos);
//}
//bool isfull(Arr * p)//1man
//{
//	bool x;
//	x = (p->len == p->cnt) ? 1 : 0;
//	if(x)printf("full");
//	return x;
//}
//bool isEmpty(Arr p)//1 
//{
//	return (0== p.cnt) ? 1 : 0;
//}
//void show_arr(Arr * p)
//{
//	for (int i = 0; i < p->cnt; i++)
//	{
//		printf("%d--", *(p->pBase + i));
//	}
//	printf("\n");
//}
//void append_arr(Arr * p,int xx){
//	if (isfull(p))
//	{
//		printf("apend fail");
//		return;
//	}
//	*(p->cnt + p->pBase) = xx;
//	p->cnt++;
//	return;
//}
//void insert_arr(Arr * p, int xx, int pos){
//	int i,tmp, last, now;
//	if (isfull(p))
//	{
//		printf(" insert fail\n");
//		return;
//	}
//	last = p->cnt;
//	for (i = last; i >=pos-1; i--)
//	{
//		*(p->pBase + i) = *(p->pBase + i - 1);
//	}
//	p->cnt++;
//	*(p->pBase + pos - 1) = xx;
//}
//void sort_arr(Arr * p)
//{
//	int i, j,tmp;
//	for (j = 0; j<p->cnt; j++)
//	{
//		for (i = 0; i < p->cnt;i++)
//		{
//			if (*(p->pBase + i)<*(p->pBase + i+1))
//			{
//				tmp = *(p->pBase + i);
//				*(p->pBase + i)=*(p->pBase + i + 1);
//				*(p->pBase + i+1)=tmp;
//			}
//		}
//	}
//	printf("sort-"); show_arr(p);
//}
typedef struct Node
{
	int data;//数据域
	struct Node * PNext;//指针域
} NODE, *PNODE;//NODE等价于struct Node，PNODE等价于struct Node *
int getLng(PNODE pNode){
	PNODE p;
	int i = 0;
	p = pNode;
	//if (p->data == NULL)
	//{
	//	return;
	//}
	while (1)
	{
		i++;
		if (p->PNext == NULL)
		{
			return i;
		}
		p = p->PNext;
	}
	printf("getLong:%d\n", i);

}

void everyone(PNODE pNode){//遍历
	PNODE p;
	int i=0;
	p = pNode;
	//if (p->data == NULL)
	//{
	//	return;
	//}
	while (1)
	{
		//printf("%d:%d--", ++i, p->data);
		printf("%d--", p->data);
		if (p->PNext == NULL)
		{
			printf("\n"); 
			break;
		}
		p = p->PNext;
	}
}
int findpos(PNODE pNode,int pos){
	int i;
	int total = getLng(pNode);
	if (pos>total)
	{
		printf("findpos Wrong\n"); return 0;
	}
	for (i = 0; i < pos; i++)
	{
		pNode = pNode->PNext;
	}
	return pNode->data;
	//int i = 1;
	//	for (i = 1; i < pos;i++)
	//	{
	//		if (pNode->PNext == NULL)
	//		{
	//				printf("Wrong\n");
	//				return 0;
	//		}
	//		pNode = pNode->PNext;
	//	}
	//	pNode = pNode->PNext;
	//	return pNode->data;
}
void Emptylist(PNODE pNode)
{
	int i;
	int total = getLng(pNode);
	for (i = 0; i < total;i++)
	{
		pNode->data = 0;
		pNode = pNode->PNext;
	}
}
void destorylist(PNODE pNode){

}
void sortList(PNODE pNode){
	int i, j,tmp=0;
	PNODE p = pNode;
	int total = getLng(pNode);
	for (i = 0; i < total;i++)
	{
		pNode = p;
		for (i = j; j < total; j++)
		{
			if (pNode->data>pNode->PNext->data)
			{
				tmp = pNode->data;
				pNode->data = pNode->PNext->data;
				pNode->PNext->data = tmp;
			}
			pNode = pNode->PNext;
		}
	}
}
void deletePos(PNODE pNode, int pos)
{
	int i;
	int total = getLng(pNode);
	for (i = 0; i < total;i++)
	{
		if (i==pos-2)//实际i=pos-1，即i是pos前一个，删除pos就要修改i的pnext值
		{
			pNode->PNext = pNode->PNext->PNext;
		}
		pNode = pNode->PNext;
	}
}
void insertPos(PNODE pNode, int pos, int val)
{
	PNODE p = (PNODE)malloc(sizeof(Node)* 1);
	int total = getLng(pNode);
	int i;
	if (pos>total)
	{
		return;
	}
	p->data = val;
	for (i = 1; i <= total; i++)
	{
		if (i == pos-1)
		{
			pNode->PNext = pNode->PNext->PNext;
		}
		pNode = pNode->PNext;
	}
}
PNODE initList(PNODE p,int it){
	int i;
	PNODE p1,p2;
	p1 = (PNODE)malloc(sizeof(Node)* 1);
	p1->data = 3;
	p =p2= p1;
	for (i = 1; i < it;i++)
	{
		p1= (PNODE)malloc(sizeof(Node)* 1);
		p1->data = i;
		p2->PNext = p1;
		p2 = p1;
	}
	p2->PNext = NULL;
	return p;
}
int _tmain(int argc, _TCHAR* argv[])
{
	//PNODE p = NULL;
	//NODE node;
	//p = &node;
	//p->data = 9;
	//p->PNext = NULL;
	//initList(p, 6);

	PNODE p = NULL;
	p=initList(p, 6);
	everyone(p);

	//char arr[] = "abcdefgh";
	//char * ptr = arr;
	//printf("%c,%c\n", *(++ptr), *(ptr));
	//ptr = arr;
	//printf("%c,%c\n", *(ptr++), *(ptr));
	//ptr = arr;
	//printf("%c,%c\n", *(ptr), *(++ptr));
	//ptr = arr;
	//printf("%c,%c\n", *(ptr), *(ptr++));

	//Arr myarr, *p;
	//p = &myarr;
	//init_arr(p,11, 4);
	//show_arr(p);
	//isfull(p);
	//append_arr(p, 100);
	//show_arr(p);
	//append_arr(p, 101);
	//show_arr(p);
	//insert_arr(p, 27, 2);
	//show_arr(p);
	//sort_arr(p);
	//insert_arr(p, 28, 3);
	//show_arr(p);
	//insert_arr(p, 29, 3);
	//show_arr(p);
	//insert_arr(p, 30, 4);
	//show_arr(p);
	//insert_arr(p, 31, 4);
	//show_arr(p);
	//sort_arr(p);
	getchar();
	return 0;
}

